package cn.edu.scau.cmi.huangxiyan.client;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.huangxiyan.adapter.ClassAdapter;
import cn.edu.scau.cmi.huangxiyan.adapter.ScoreOperation;
import cn.edu.scau.cmi.huangxiyan.adapter.Student;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class AdapterClient extends Application {
	public List<Student> studentList = new ArrayList<Student>();
	public List<Student> resultList = new ArrayList<Student>();

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("AdapterClient");
		VBox vBox = new VBox(15);
		vBox.setPadding(new Insets(15, 5, 5, 5));

		Label NOLabel = new Label("学生学号：");
		NOLabel.setPrefSize(70, 25);

		TextField NOTextField = new TextField("请输入学生学号");
		NOTextField.setPrefSize(530, 25);

		HBox NOHBox = new HBox(5);
		NOHBox.setPadding(new Insets(5, 0, 5, 0));
		NOHBox.getChildren().addAll(NOLabel, NOTextField);

		Label nameLabel = new Label("学生姓名：");
		nameLabel.setPrefSize(70, 25);

		TextField nameTextField = new TextField("请输入学生姓名");
		nameTextField.setPrefSize(530, 25);

		HBox nameHBox = new HBox(5);
		nameHBox.setPadding(new Insets(5, 0, 5, 0));
		nameHBox.getChildren().addAll(nameLabel, nameTextField);

		TextArea textArea = new TextArea("");
		textArea.setPrefSize(600, 200);
		textArea.setEditable(false);

		Button viewOldButton = new Button("显示排序前名单");
		viewOldButton.setPrefSize(150, 25);
		viewOldButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				for (Student student : studentList) {
					textArea.appendText(student.toString() + "\n");
				}
			}
		});

		Button viewNewButton = new Button("显示排序后名单");
		viewNewButton.setPrefSize(150, 25);
		viewNewButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				for (Student student : resultList) {
					textArea.appendText(student.toString() + "\n");
				}
			}
		});

		Button classButton = new Button("用类适配器添加");
		classButton.setPrefSize(150, 25);
		classButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				boolean intFlag = canParseInt(NOTextField.getText());

				if (intFlag) {
					boolean NOFlag = isNOUnique(studentList, Integer.parseInt(NOTextField.getText()));
					if (NOFlag) {
						Student newStudent = new Student(Integer.parseInt(NOTextField.getText()),
								nameTextField.getText());
						studentList.add(newStudent);

						ScoreOperation classAdapter = new ClassAdapter();
						resultList = classAdapter.sort(studentList);
						for (Student student : resultList) {
							textArea.appendText(student.toString() + "\n");
						}
						textArea.appendText("\n" + newStudent.toString() + " 该学生用类适配器添加。\n");
					} else {
						textArea.appendText("该学号已存在，请输入新的学生学号！\n");
					}
				} else {
					textArea.appendText("请输入正确的学生学号（0 - 2147483648且为整数）！\n");
				}
			}
		});

		Button objectButton = new Button("用对象适配器添加");
		objectButton.setPrefSize(150, 25);
		objectButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				boolean intFlag = canParseInt(NOTextField.getText());

				if (intFlag) {
					boolean NOFlag = isNOUnique(studentList, Integer.parseInt(NOTextField.getText()));
					if (NOFlag) {
						Student newStudent = new Student(Integer.parseInt(NOTextField.getText()),
								nameTextField.getText());
						studentList.add(newStudent);

						ScoreOperation classAdapter = new ClassAdapter();
						resultList = classAdapter.sort(studentList);
						for (Student student : resultList) {
							textArea.appendText(student.toString() + "\n");
						}
						textArea.appendText("\n" + newStudent.toString() + " 该学生用对象适配器添加。\n");
					} else {
						textArea.appendText("该学号已存在，请输入新的学生学号！\n");
					}
				} else {
					textArea.appendText("请输入正确的学生学号（0 - 2147483648且为整数）！\n");
				}
			}
		});

		HBox buttonHBox = new HBox(5);
		buttonHBox.setPadding(new Insets(5, 0, 5, 0));
		buttonHBox.getChildren().addAll(viewOldButton, viewNewButton, classButton, objectButton);

		vBox.getChildren().addAll(NOHBox, nameHBox, textArea, buttonHBox);
		Scene scene = new Scene(vBox, 600, 300);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public boolean isNOUnique(List<Student> studentList, int NO) {
		for (Student student : studentList) {
			if (student.getNO() == NO) {
				return false;
			}
		}

		return true;
	}

	public boolean canParseInt(String str) {
		try {
			@SuppressWarnings("unused")
			int temp = Integer.parseInt(str);
		} catch (Exception e) {
			return false;
		}

		return true;
	}

}
